﻿#include "brolist.h"
#include "ui_brolist.h"
#include "chat.h"
#include "widget.h"

#include <QDebug>
#include <QMessageBox>

BroList::BroList(QTcpSocket *m_tcp, QString id_, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BroList)
{
    ui->setupUi(this);
    m_t = m_tcp;
    uid = id_;
    setWindowTitle("聊天列表");
    ui->label_2->setText("在线人数:0");
    w = new QListWidget(ui->widget);
    QPixmap icon("://qq.jfif");
    icon = icon.scaled(ui->head->width(), ui->head->height());
    ui->head->setPixmap(icon);
    ui->textEdit->append("个性签名：");
    ui->textEdit->append("  这个家伙很懒，什么也没有。");
    w->setFixedSize(320, 400);
//    //设置背景
//    QPalette pal = this->palette();
//    pal.setBrush(QPalette::Background,QBrush(QPixmap("://p4.jpeg")));
//    setPalette(pal);
    sql = new Data;
    QSqlQuery query(sql->db);
    query.exec("select * from data");
    while (true == query.next()) {
        qDebug() << query.value(0).toString()
                 << query.value(1).toString()
                 << query.value(2).toString();
        sql->id.append(query.value(0).toString());
        sql->passwd.append(query.value(1).toString());
        sql->name.append(query.value(2).toString());
    }
    QString myname;
    QString myid;
    for(int i = 0;i<sql->name.size();i++)
    {
        if (QString::compare(sql->id[i], uid) == 0) {
            ui->name->setText("昵称："+sql->name[i]);
            myname = sql->name[i];
            myid = sql->id[i];
        }
        QListWidgetItem *item = new QListWidgetItem;
        item->setSizeHint(QSize(400,50));
        //size < 5 is group chat.
        if (sql->id[i].size() < 5) {
            item->setText(sql->name[i]);
            item->setToolTip("group chat ^_^");
            item->setIcon(QIcon(":/group.jpg"));
        } else {
            item->setText(sql->name[i]+"(离线)");
            item->setToolTip("good friend ^_^");
            item->setIcon(QIcon(":/qq.jfif"));
        }
        item->setHidden(true);
        w->addItem(item);

    }

    connect(w, &QListWidget::currentRowChanged, this, [=](int index){
        qDebug() << "index: " << index;
        QString uname = sql->name[index];
        QString uid = sql->passwd[index];
        Chat *chat = new Chat(myname, myid, uname, uid);
        chat->show();
        //交互 chat
        connect(chat, &Chat::sendId_Msg, this, [=](QString from_id, QString to_id, QString msg){
           emit sendId_Msg(from_id, to_id, msg);
        });
        connect(this, &BroList::sendMsg, chat, &Chat::recvMsg);
        connect(this, &BroList::sendFileText, chat, &Chat::recvFileText);
        connect(this, &BroList::sendGroupMsg, chat, &Chat::recvGroupMsg);

        //file传输
        connect(chat, &Chat::sendFileContent, this, [=](QString myid_, QString uid_, int size, QString data){
            emit sendFileContent(myid_, uid_, size, data);
        });
        //禁言等列表
        connect(chat, &Chat::sendTp, this, [=](QString tp){
            int flag = 0;
            for (int i=0;i<sql->id.size();i++) {
                if (QString::compare(tp, sql->id[i])==0) {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                QMessageBox::information(this, "提示", "群里不存在此人");
            } else {
                emit sendTp(tp);
            }
        });

        connect(chat, &Chat::sendOpenSay, this, [=](QString openSay){
            int flag = 0;
            for (int i=0;i<sql->id.size();i++) {
                if (QString::compare(openSay, sql->id[i])==0) {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                QMessageBox::information(this, "提示", "群里不存在此人");
            } else {
                emit sendOpenSay(openSay);
            }
        });

        connect(chat, &Chat::sendNoSay, this, [=](QString noSay){
            int flag = 0;
            qDebug() << "+++++++++" << noSay;
            for (int i=0;i<sql->id.size();i++) {
            qDebug() << "+++++++++" << sql->id[i];
                if (QString::compare(noSay, sql->id[i])==0) {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                QMessageBox::information(this, "提示", "群里不存在此人");
            } else {
                emit sendNoSay(noSay);
            }
        });


    });
    connect(ui->exit, &QPushButton::clicked, this, &BroList::close);

}

BroList::~BroList()
{
    delete ui;
}


void BroList::on_update_clicked()
{
    int type = 4;
    m_t->write((char*)&type, 4);
}

void BroList::recvPcnt(int cnt)
{
    ui->label_2->setText("在线人数:"+QString::number(cnt));
    uids.clear();
    names.clear();
}

void BroList::recvId(QString uid)
{
    uids.push_back(uid);
    for (int i=0;i<sql->id.size(); i++) {
        if (QString::compare(uid, sql->id[i]) == 0) {
            names.push_back(sql->name[i]);
            break;
        }
    }
}

void BroList::recvListOver()
{
    for (int i = 0;i < w->count();i++) {
        int flag_ = 0;
        if (sql->id[i].size() < 5) continue;
        for (int j = 0;j < names.size(); j++) {
            if (QString::compare(sql->name[i], names[j]) == 0) {
                w->item(i)->setText(names[j]+"(在线)");
                flag_ = 1;
                break;
            }
        }
        if (!flag_) w->item(i)->setText(sql->name[i]+"(离线)");
    }
}

void BroList::recvMsg(QString msg)
{
    emit sendMsg(msg);
}

void BroList::recvFileText(QString data_)
{
    emit sendFileText(data_);
}

void BroList::recvGroupMsg(QString from, QString msg)
{
    emit sendGroupMsg(from, msg);
}






